package com.spsoft.core.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 老项目密码加密， 暂时加密token使用
 *
 * @author cdl
 * @date 2023/11/08
 */
public class DESCrypt {
    private SecretKey key = null;
    private Cipher encipher = null;
    private Cipher decipher = null;
    private BASE64Decoder base64Decoder = new BASE64Decoder();
    private BASE64Encoder base64Encoder = new BASE64Encoder();
    private static final String CRYPT_KEY = "rO0ABXNyAB5jb20uc3VuLmNyeXB0by5wcm92aWRlci5ERVNLZXlrNJw12hVomAIAAVsAA2tleXQAAltCeHB1cgACW0Ks8xf4BghU4AIAAHhwAAAACCyDuaGUv2jy";

    public DESCrypt() throws Exception {
        this.init();
    }

    private void init() throws Exception {
        try {
            ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(this.base64Decoder.decodeBuffer("rO0ABXNyAB5jb20uc3VuLmNyeXB0by5wcm92aWRlci5ERVNLZXlrNJw12hVomAIAAVsAA2tleXQAAltCeHB1cgACW0Ks8xf4BghU4AIAAHhwAAAACCyDuaGUv2jy")));
            this.key = (SecretKey)objIn.readObject();
            objIn.close();
            objIn = null;
        } catch (IOException var3) {
            throw var3;
        }

        try {
            this.encipher = Cipher.getInstance("DES");
            this.encipher.init(1, this.key);
            this.decipher = Cipher.getInstance("DES");
            this.decipher.init(2, this.key);
        } catch (Exception var2) {
            System.out.println("生成加密引擎失败!");
            throw var2;
        }
    }



    public String encrypt(byte[] data) {
        try {
            return this.base64Encoder.encode(this.encipher.doFinal(data));
        } catch (Exception var3) {
            System.out.println("encrypt false!");
            return null;
        }
    }

    public byte[] decrypt(String data) {
        try {
            return this.decipher.doFinal(this.base64Decoder.decodeBuffer(data));
        } catch (Exception var3) {
            System.out.println("decrypt false!");
            return null;
        }
    }



    /**
     * 编码
     *
     * @param content
     * @return
     */
    public  String encode(byte[] content) {
        return new sun.misc.BASE64Encoder().encode(content);
    }

    /**
     * 解码
     *
     * @param source
     * @return
     */
    public  byte[] decode(String source) {
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            return decoder.decodeBuffer(source);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


//    public static void main(String[] args) throws Exception {
//        DESCrypt crypt = new DESCrypt();
//        String str = "ncl1234:2342:23423:234:234:324234243";
//        str=crypt.encrypt(str.getBytes());
//        String mi =crypt.encode(str.getBytes());
//        String str2 = new String( crypt.decode(mi));
//        System.out.println(str2);
//        str2=new String(crypt.decrypt(str2));
//        System.out.println(mi);
//        System.out.println(str2);
//
//    }
}
